<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 视频网课管理
 */
class Course extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

	$this->school_id = $this->user_data['school_id'];
	$this->teacher_id = $this->user_data['teacher_id'];
	$this->uid = $this->user_data['id'];
	$this->load->model('videos');


	// $this->left_menu_min = array("menu" => "教务管理", "curr" => '/offline_course', "list" => array("作业管理" => "/classes/home_work_list", "面授课" => "/course_banke", "班级管理" => "/classes", "学员管理" => "/student", "出勤管理" => "/chuqin/today_class", "老师管理" => "/teacher", "课程分类" => "/offline_course/cate", "课程科目" => "/offline_course", "教室管理" => "/school_room", "校区管理" => "/school_campus"));
	// $this->smarty->assign("left_menu_min", $this->left_menu_min);
	// $this->smarty->assign("menu", 'classes');
		$parent_id = 3;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "课程管理", "curr" => '/course', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", "course");

	}


	/**
	 * 视频课
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index($status = 10,$sort = 0,$asc_des = 0,$search = 0, $cate_id=0, $kemu=0, $limit = 0)
	{
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		$this->load->model("Courses");
		$data = array( 'status' => $status, 'cate_id' => $cate_id, 'kemu'=>$kemu, 'type' => 1, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 10;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->model('Anchors');
		$result = $this->Anchors->get_list_ofpage($data['limit'], $offset,$data);
		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/

		$subject = $this->Courses->get_course_cate_subject($cate_id);
		$cate = $this->Courses->get_cate(1);
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/index/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);

		$this->smarty->display("course/index.html");
	}

	public function edit_shipinke($id){
		$token = get_qiniu_token('VIMG');
		$post_data = $this->input->post();
		if($post_data){
			if($post_data['title'] == ""){
				echo  "标题不能为空";exit;
			}
			$image = $post_data['img_url'];
			if(!empty($image)){
				$post_data['course_img'] = $image[0];
				unset($image[0]);
				if(!empty($image)){
					$post_data['course_imgs'] = json_encode($image);
				}
			}
			unset($post_data['img_url']);
			$this->db->where('id',$post_data['id']);
			$result = $this->db->update('course',$post_data);
			if($result){
				echo 1;exit;
				$data['uid'] = $this->uid;
				$data['cate_id'] = $post_data['cate_id'];
				$data['course_id'] = $this->db->insert_id();
				$data['teacher_id'] = $this->teacher_id;
				$data['school_id'] = $this->school_id;
				$data['type'] = 1;
			}
		}
		$this->load->model("Courses");
		$course = $this->Courses->get_course_info($id);
		if($course['vid'] > 0){
			$video_info = get_field_by_where("video", "title", "id='{$course['vid']}'");
			$this->smarty->assign("video_title", $video_info['title']);
		}
		$course_chapter = $this->Courses->get_chapter_list($id);
		$course_imgs = json_decode($course['course_imgs'],true);
		$cate_id = !empty($course['cate_id']) ? $course['cate_id'] : 0;
		$subject = $this->Courses->get_cate_subject($cate_id);
		$teacher = $this->Courses->get_teacher($this->user_data['school_id']);
		$cate = $this->Courses->get_cate(1);

		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("course", $course);
		$this->smarty->assign("chapter", $course_chapter);
		$this->smarty->assign("img_url", $course_imgs);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$this->smarty->display("course/add_video_course.html");
	}

	function delete_shipinke($id = 0){
		if(empty($id)){
			show_message("/course", "非法操作");
		}else{
			delete_data((array)$id, "course");
			show_message("/course");
		}
	}

	/**
	 * 安排课程目录
	 * @param  [type] $course_id [description]
	 * @return [type]            [description]
	 */
	public function edit_video_subject($course_id)
	{
		$this->smarty->assign("course_id", $course_id);
		$this->load->model("Courses");
		$course = $this->Courses->get_course_info($course_id);
		if(empty($course)) {
			header("Location:/course");exit;
		}
		$course_chapter = $this->Courses->get_chapter_list($course_id);
		$this->smarty->assign("chapter", $course_chapter);

		$this->smarty->assign("post_url", "/course/shipinke_publish");

		$this->smarty->display("course/edit_video_subject.html");
	}

	function shangjia(){
		$this->load->model("Courses");
		$post_data = $this->input->post();

		$check = $this->Courses->check_is_sj($post_data['id'],$this->school_id);
		if($post_data['id'] > 0 ){
			// 更新上架状态(下架)
			if($check['is_fenxiao'] == 1){
				$this->db->where('id', $check['id']);
				$result = $this->db->delete('course_shelves');
			}else{
				$sql = "update {$this->db->dbprefix}course_shelves set status = 1-status where id ={$check['id']}";
				$this->db->query($sql);
			}
			echo 2;exit;
			// print_r($check);exit;
			// if($check){
			// 	// $this->db->where('course_id',$post_data['id']);
			// 	// $result = $this->db->delete('course_shelves');
			//
			// 	if($result){
			// 		echo 2;exit;
			// 	}else{
			// 		echo 0;exit;
			// 	}
			// }else{
			// 	$result = $this->Courses->get_course_info($post_data['id']);
			// 	$data['uid'] = $result['uid'];
			// 	$data['cate_id'] = $result['cate_id'];
			// 	$data['course_id'] = $result['id'];
			// 	$data['teacher_id'] = $result['teacher_id'];
			// 	if($result['school_id'] == 0){
			// 		$data['is_fenxiao'] = 1;
			// 	}
			// 	$data['school_id'] = $this->school_id;
			// 	$data['type'] = $result['type'];
			// 	$result = $this->db->insert('course_shelves',$data);
			// 	if($result){
			// 		echo 1;exit;
			// 	}else{
			// 		echo 0;exit;
			// 	}
			// }
		}


	}

	/**
	 * 发布视频课
	 * @return [type] [description]
	 */
	public function shipinke_publish(){
		$token = get_qiniu_token('VIMG');
		$post_data = $this->input->post();
		if(isset($post_data['img_url'])){
			foreach ($post_data['img_url'] as $key => $value) {
				$post_data['img_url'][$key] = get_qiniu_url($value);
			}
		}
		$this->load->model("Courses");
		if($post_data){
			if($post_data['title'] == ""){
				echo  "标题不能为空";exit;
			}

			/*for ($i=0; $i < count($data['img_url']); $i++) {
				$post_data['img_url'][i] = get_qiniu_url($post_data['img_url'][i]);
			}*/
			if(empty($post_data['id'])){
				$course_id = $this->Courses->publish_course_video($this->user_data['school_id'],$this->uid,$post_data);
			}else{
				$course['title'] = $post_data['title'];
				$course['cate_id'] = $post_data['cate_id'];
				$course['subject_id'] = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
				$course['teacher_id'] = $post_data['teacher_id'];
				if(!empty($post_data['img_url'])){
					$course['course_img'] = !empty($post_data['img_url'])?$post_data['img_url'][0]:'';
					$course['course_imgs'] = !empty($post_data['img_url'])?json_encode($post_data['img_url']):'';
				}
		      	$course['detail'] = $post_data['detail'];
		      	$course['price'] = $post_data['price'];
		      	$course['course_time'] = $post_data['course_time'];
		      	$course['effective_time'] = $post_data['effective_time'];
		      	$course['suitable'] = $post_data['suitable'];
		      	$course['learn_what'] = $post_data['learn_what'];
		      	$course['is_recomend'] = !empty($post_data['is_recomend']) ? $post_data['is_recomend'] : 0;
		      	$course['vid'] = !empty($post_data['vid']) ? $post_data['vid'] : 0;
				$this->db->where("id", $post_data['id']);
				$this->db->update("course", $course);

				$shelves_data['cate_id'] = $post_data['cate_id'];
				$shelves_data['teacher_id'] = $post_data['teacher_id'];
				$shelves_data['fx_price'] = $post_data['price'];
				$shelves_data['commission'] = $post_data['commission'];
				$shelves_data['is_free'] = $post_data['is_free'];
				$this->db->where("course_id", $post_data['id']);
				$this->db->update("course_shelves", $shelves_data);
				$course_id = $post_data['id'];
			}

			header("Location:/course/edit_video_subject/{$course_id}");exit;
		}

		//$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher($this->user_data['school_id']);
		$cate = $this->Courses->get_cate(1);//录播课分类


		//$this->smarty->assign("subject", $subject);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", "/course/shipinke_publish");
		$this->smarty->display("course/add_video_course.html");
	}


	function add_video_chapter(){
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id']> 0){
			$data['name'] = isset($post_data['name'])?$post_data['name']:'';
			$data['vid'] = isset($post_data['vid'])?$post_data['vid']:0;
			$data['describe'] = isset($post_data['describe'])?$post_data['describe']:'';
			$data['course_id'] = isset($post_data['course_id'])?$post_data['course_id']:'';
			$data['is_free'] = isset($post_data['is_free'])?$post_data['is_free']:0;
			$this->db->where('id',$post_data['id']);
			$this->db->update('course_chapter',$data);
			echo json_encode($post_data);
			exit;
		}

		$result = $this->db->insert('course_chapter',$post_data);
		$res = array("id" => $this->db->insert_id(), "pid" => 0);
		if(!empty($post_data['pid'])){
			$res['pid'] = $post_data['pid'];
		}
		echo json_encode($res);
	}


	function del_video_chapter(){
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id'] > 0){
			$this->db->where('id',$post_data['id']);
			$result = $this->db->delete('course_chapter');
			if($result){
				$this->db->where('pid',$post_data['id']);
            	$result = $this->db->delete('course_chapter');
            	if($result){
            		echo 1;exit;
            	}else{
            		echo 0;exit;
            	}
			}else{
				echo 0;exit;
			}
		}
	}



	function video($review = 0,$sort = 0,$asc_des = 0,$search = 0,$zid=0,$subject_id = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['review']) ? $post_data['review'] : 0;
			$zid = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
			$subject_id = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 11;
		}

		$data = array( 'review' => $review, 'cate_id'=>$zid, 'subject_id'=>$subject_id, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 12;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->videos->get_list_ofpage($data['limit'], $offset,$data);
		$this->load->model("Courses");
		$subject = $this->Courses->get_course_cate_subject($zid);
		$cate = $this->Courses->get_cate(1);
		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/

		$this->load->library('pagination');
		$page_config['base_url'] = '/course/video/'.$data['review'].'/'.$data['sort'].'/'.$data['cate_id'].'/'.$data['subject_id'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("video", $result['result']);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->display("course/video_list.html");
	}


	function select_video($cate_id = 0, $subject_id = 0, $search = 0,$limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = !empty($post_data['search']) ? $post_data['search'] : 0;
			$cate_id = !empty($post_data['cate_id']) ? $post_data['cate_id'] : 0;
			$subject_id =  !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 6;
		}
		$this->load->model("Courses");
		$data = array( 'cate_id' => $cate_id, 'search'=>$search, 'subject_id' => $subject_id);
		$this->smarty->assign("action", '视频列表');
		$offset = 12;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->videos->get_list_ofpage($limit, $offset, $data);

		$this->load->library('pagination');
		$page_config['base_url'] = '/course/select_video/'.$cate_id.'/'.$subject_id.'/'.$search;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("page_total", ceil($result['count']/$offset));
		
		$this->smarty->assign("video", $result['result']);

		$cate = $this->Courses->get_cate();
		$subject = $this->Courses->get_course_cate_subject($cate_id);
		
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("pagination", $pagination);

		$this->smarty->display("course/select_video_list.html");
	}


	function add_video(){
		$this->load->model("Courses");
		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id'] > 0){

		}
		//$token = get_qiniu_token('VIMG');
		//if(isset($post_data['image_url'])) $post_data['image_url'] = get_qiniu_url($data['image_url']);
		unset($post_data['id']);
		if(isset($post_data['title']) && $post_data['title'] == ""){
			echo "请输入标题";exit;
		}
		if(isset($post_data['description']) && $post_data['description'] == ""){
			echo "请视频描述";exit;
		}
		if(isset($post_data['image_url']) && $post_data['image_url'] != "") {
			$post_data['image_url'] = get_qiniu_url($post_data['image_url']);
		}
		if(isset($post_data['video_url']) && $post_data['video_url'] != "") {
			$post_data['video_url'] = get_qiniu_url('gABTEYXqTnt5OuxgtTAPDtIwyC4=/'.$post_data['video_url'], "VIDEO");
		}
		if(isset($post_data['description']) && isset($post_data['title'])){
			$post_data['add_time'] = time();
			$post_data['school_id'] = $this->user_data['school_id'];
			$post_data['teacher_id'] = $this->user_data['teacher_id'];
			unset($post_data['file']);
			$result = $this->db->insert('video',$post_data);
			if($result){
				redirect("/course/video");
			}
		}
		$cate = $this->Courses->get_cate(1);//录播课分类

		//http://v.polyv.net/uc/services/rest?method=getById&readtoken=etuB1n9Euo-n3qXc4od09-3bHaAqk11j&vid=d74867135f5c06a642ac3361867a7cad_d

		$this->smarty->assign("token", '');
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("post_url", "/course/add_video");
		$this->smarty->display("course/add_video.html");
	}

	/**
	 * 编辑视频
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	function edit_video($id = 0){
		$id = intval($id);
		$this->load->model("Courses");
		if($id > 0 ){
			$cate = $this->Courses->get_cate(1);//录播课分类

			$token = get_qiniu_token('VIMG');
			$videos_info = $this->videos->get_video_info($id);
			$subject = $this->Courses->get_cate_subject($videos_info[0]['cate_id']);
			$this->smarty->assign("token", $token);
			$this->smarty->assign("cate", $cate);
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("videos_info", $videos_info[0]);

			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->smarty->display("course/add_video.html");
		}


		$post_data = $this->input->post();
		if(isset($post_data['id']) && $post_data['id'] > 0){
			$post_data['edit_time'] = time();
			if(isset($post_data['image_url']) && $post_data['image_url'] != ''){
				if(isset($post_data['image_url'])) $post_data['image_url'] = get_qiniu_url($post_data['image_url']);
			}
			if(isset($post_data['file'])) unset($post_data['file']);
			$this->db->where('id', $post_data['id']);
			$result = $this->db->update('video', $post_data);
			if($result){
				//show_message($_SERVER['HTTP_REFERER'], "视频修改成功");exit;
			}else{
				//show_message($_SERVER['HTTP_REFERER'], "视频修改失败");exit;
			}
		}
	}



	/**
	 * 课程分类
	 * @param  integer $search [description]
	 * @param  integer $limit  [description]
	 * @return [type]          [description]
	 */
	function cate_list($search = 0,$limit = 0){
		$offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 5;
		}
		$this->load->model("Courses");
		$list = $this->Courses->get_cate_list($search,1,$offset,$limit);
		$data = array( 'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频课分类列表');

		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/cate_list/'.$data['search'].'/';
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("current_url", $current_url);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("data", $data);

		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course/course_cate_list.html");
	}


	function cate_publish(){
		$post_data = $this->input->post();
		if($post_data){
			unset($post_data['id']);
			if($post_data['name'] == ""){
				echo  "标题不能为空";exit;
			}
			if($post_data['sort'] == ""){
				echo  "排序不能为空";exit;
			}
			$post_data['type'] = 1;
			$post_data['school_id'] = $this->school_id;
			$post_data['teacher_id'] = $this->teacher_id;
			$post_data['add_time'] = time();
			$result = $this->db->insert('course_cate',$post_data);
			if($result){
				redirect("/course/cate_list");
			}
		}
		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher($this->user_data['school_id']);

		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("post_url", "/course/cate_publish");
		$this->smarty->display("course/add_course_cate.html");
	}


	public function edit_course_cate($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->load->model("Courses");
			$post_data = $this->input->post();
			if(!empty($post_data['ico'])){
				$post_data['ico'] = get_qiniu_url($post_data['ico']);
				unset($post_data['file']);
			}
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course_cate',$post_data);
				if($result){
					redirect("/course/cate_list");
				}
			}
			$result = $this->Courses->get_cate_info($id);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

			$this->smarty->display("course/add_course_cate.html");

		}
	}

	/**
	 * 编辑网课科目
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function edit_course_cate_subject($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->load->model("Courses");
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course_cate_subject',$post_data);
				if($result){
					redirect("/course/cate_subject_list");
				}
			}
			$result = $this->Courses->get_cate_subject_info($id);


			$cate = $this->Courses->get_cate($result['type']);

			$this->smarty->assign("cate", $cate);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

			$this->smarty->display("course/add_course_cate_subject.html");

		}
	}

	/**
	 * 发布网课的科目
	 * @return [type] [description]
	 */
	function cate_subject_publish(){
		$post_data = $this->input->post();

		if($post_data){
			unset($post_data['id']);
			if($post_data['name'] == ""){
				echo  "标题不能为空";exit;
			}
			if($post_data['sort'] == ""){
				echo  "排序不能为空";exit;
			}
			$post_data['type'] = 1;
			$post_data['school_id'] = $this->school_id;
			$post_data['teacher_id'] = $this->teacher_id;
			$post_data['add_time'] = time();
			$result = $this->db->insert('course_cate_subject',$post_data);
			if($result){
				redirect("/course/cate_subject_list");
			}
		}

		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher();

		$cate = $this->Courses->get_cate(1);//默认取录播的分类


		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("post_url", "/course/cate_subject_publish");
		$this->smarty->display("course/add_course_cate_subject.html");
	}

	/**
	 * ajax获取课程分类
	 *
	 * @param  [type] $type [description]
	 * @return [type]       [description]
	 */
	public function get_cate_list_ajax($type)
	{
		$this->load->model("Courses");
		$cate = $this->Courses->get_cate($type);//取分类
		print_r(json_encode($cate));exit;
	}


	/**
	 * 网课科目list
	 * @param  integer $search [description]
	 * @param  integer $type   [description]
	 * @param  integer $limit  [description]
	 * @return [type]          [description]
	 */
	function cate_subject_list($search = 0,$cate = 0,$limit = 0){
		$offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$cate = $post_data['cate'] ? $post_data['cate'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 5;
		}
		$this->load->model("Courses");
		//取视频课科目
		$list = $this->Courses->get_cate_subject_list($search,$cate,1,$offset,$limit);

		//获取视频课分类
		$cate_list = $this->Courses->get_cate(1);//默认取录播的分类
		$this->smarty->assign('cate_list',$cate_list);


		$data = array( 'search' => $search,'cate'=>$cate,'limit' => $limit);
		$this->smarty->assign("action", '视频课科目列表');

		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/cate_subject_list/'.$data['search'].'/'.$data['cate'].'/';
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("current_url", $current_url);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("data", $data);

		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course/course_cate_subject_list.html");
	}


	function get_cate_subject(){
		$this->load->model("Courses");
		$post_data = $this->input->post();
		$result = $this->Courses->get_course_cate_subject($post_data['id']);
		echo json_encode($result);
	}


	function delete_course_cate_subject($id){
		if($id > 0){
			$this->db->where('id',$id);
			$result = $this->db->delete('course_cate_subject');
			if($result){
				redirect("/course/cate_subject_list");
			}
		}

	}

	function delete_course_cate($id){
		if($id > 0){
			$this->db->where('id',$id);
			$result = $this->db->delete('course_cate');
			if($result){
				redirect("/course/cate_list");
			}
		}

	}

	function comment_list($course_id = 0, $limit = 0){

		$course_info = get_field_by_where("course", "*", "id='{$course_id}'");
		if(!$course_info){
			show_message("/course", "课程不存在");exit;
		}

		if($course_info['type'] == 3){
			$curr = '/course_banke';
		}elseif($course_info['type'] == 2){
			$curr = '/course_zhibo';
		}else{
			$curr = '/course';
		}
		$parent_id = 3;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "课程管理", "curr" => $curr, "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);


		$this->load->model('Courses');
        $comment_list = $this->Courses->get_course_comment_list($course_id, $limit, $this->offset);

        $this->load->library('pagination');
        $page_config['base_url'] = '/course/comment_list/'.$course_id;
        $page_config['total_rows'] = $comment_list['total_count'];
        $page_config['per_page'] = $this->offset;
        $page_config['uri_segment'] = 4;
        $this->pagination->initialize($page_config);
        $pagination = $this->pagination->create_links();
        $this->smarty->assign("pagination", $pagination);

        $this->smarty->assign("comment_list", $comment_list['result']);
        $this->smarty->display("course/comment_list.html");
	}
}
